package com.lcxw.hxzb.friendcircle.project.system.controller;

import java.util.List;

import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdDynamic;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdDynamicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdLike;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdLikeService;
import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;

/**
 * 动态点赞Controller
 *
 * @author jjp
 * @date 2020-07-20
 */
@RestController
@Api("动态点赞")
@RequestMapping("/system/like")
public class YwpdLikeController extends BaseController {

    @Autowired
    private IYwpdLikeService ywpdLikeService;

    @Autowired
    private IYwpdDynamicService ywpdDynamicService;

    /**
     * 查询动态点赞列表
     */
    @ApiOperation("查询动态点赞列表")
    @GetMapping("/list")
    public TableDataInfo list(YwpdLike ywpdLike) {
        startPage();
        List<YwpdLike> list = ywpdLikeService.selectYwpdLikeList(ywpdLike);
        return getDataTable(list);
    }

    /**
     * 获取动态点赞详细信息
     */
    @ApiOperation("获取动态点赞详细信息")
    @GetMapping(value = "/{likeId}")
    public AjaxResult getInfo(@PathVariable("likeId") Long likeId) {
        return AjaxResult.success(ywpdLikeService.selectYwpdLikeById(likeId));
    }

    /**
     * 新增动态点赞
     */
    @ApiOperation("新增动态点赞")
    @PostMapping
    public AjaxResult add(@RequestBody YwpdLike ywpdLike) {
        return toAjax(ywpdLikeService.insertYwpdLike(ywpdLike));
    }

    /**
     * 修改动态点赞
     */
    @ApiOperation("修改动态点赞")
    @PutMapping
    public AjaxResult edit(@RequestBody YwpdLike ywpdLike) {
        return toAjax(ywpdLikeService.updateYwpdLike(ywpdLike));
    }

    /**
     * 删除动态点赞
     */
    @ApiOperation("删除动态点赞")
    @DeleteMapping("/{likeIds}")
    public AjaxResult remove(@PathVariable Long[] likeIds) {
        return toAjax(ywpdLikeService.deleteYwpdLikeByIds(likeIds));
    }

    @Transactional
    @PostMapping("insertLikeByUser")
    public AjaxResult insertLikeByUser(@RequestBody YwpdLike ywpdLike) {
        List<YwpdLike> ywpdLikes = ywpdLikeService.selectYwpdLikeList(ywpdLike);
        if (ywpdLikes.size() != 0) {
            return AjaxResult.error("重复点赞");
        }
        int i = ywpdLikeService.insertYwpdLike(ywpdLike);
        if (i == 1) {
            YwpdDynamic ywpdDynamic = new YwpdDynamic();
            ywpdDynamic.setDynamicId(ywpdLike.getDynamicId());
            ywpdDynamicService.insertLikeByUser(ywpdDynamic);
            return AjaxResult.success("点赞成功");
        } else {
            return AjaxResult.error("点赞错误");
        }
    }

    @Transactional
    @PostMapping("deleteLikeByUser")
    // dynamicId userId
    public AjaxResult deleteLikeByUser(@RequestBody YwpdLike ywpdLike) {

        int i = ywpdLikeService.deleteYwpdLikeById(ywpdLike);
        if (i == 1) {
            YwpdDynamic ywpdDynamic = new YwpdDynamic();
            ywpdDynamic.setDynamicId(ywpdLike.getDynamicId());
            ywpdDynamicService.deleteLikeByUser(ywpdDynamic);
            return AjaxResult.success("取消点赞成功");
        } else {
            return AjaxResult.error("取消失败");
        }
    }
}
